package com.luocj.elephant.demo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.luocj.elephant.demo.utils.Utils;


public class ProgressViewWithColor extends View {

    private static final String     TAG = ProgressViewWithColor.class.getSimpleName();
    private int                     width;
    private int                     height;
    private Paint                   mPaint;

    public ProgressViewWithColor(Context context) {
        this(context, null);
    }

    public ProgressViewWithColor(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressViewWithColor(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = Utils.getScreenWidth(getContext());
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = Utils.dip2px(getContext(), 30);
        }

        setMeasuredDimension(width, height);

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.TRANSPARENT);
        canvas.translate(0, getHeight());
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();

        int screenWidth = Utils.getScreenWidth(getContext());
        mPaint.setStrokeCap(Paint.Cap.ROUND);

        mPaint.setStrokeWidth(getHeight());
        canvas.drawLine(paddingLeft, -getHeight() / 2, (float) (screenWidth * 0.3), -getHeight() / 2, mPaint);
        mPaint.setColor(Color.YELLOW);
        canvas.drawLine(paddingLeft + (float) (screenWidth * 0.3)-mPaint.getStrokeWidth() , -getHeight() / 2, (float) (screenWidth * 0.5), -getHeight() / 2, mPaint);
        mPaint.setColor(Color.BLUE);
        canvas.drawLine(paddingLeft + (float) (screenWidth * 0.5)-mPaint.getStrokeWidth() , -getHeight() / 2, (float) (screenWidth * 0.8), -getHeight() / 2, mPaint);
        mPaint.setColor(Color.rgb(253,84,110));
        canvas.drawLine(paddingLeft + (float) (screenWidth * 0.8)-mPaint.getStrokeWidth()    , -getHeight() / 2, (float) (screenWidth * 0.95), -getHeight() / 2, mPaint);
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
    }
}
